package study.sso.jwt.support.jwt;

import lombok.Getter;
import lombok.Setter;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Payload 用来存放实际需要传递的数据，JWT 规定的7个官方字段，供选用:
 * ==============================
 * jti (JWT ID)：编号
 * ==============================
 * sub (Subject)：主题
 * iss (Issuer)：签发者
 * aud (Audience)：接收者
 * ==============================
 * exp (Expiration time)：过期时间
 * nbf (Not Before)：生效时间
 * iat (Issued At)：签发时间
 * ==============================
 */
public class Payload {

    private Map<String, Object> _data = new HashMap<>();

    /* 编号 */
    public static final String KEY_ID = "jti";

    /* 主题 */
    public static final String KEY_SUB = "sub";
    /* 签发人 */
    public static final String KEY_ISS = "iss";
    /* 接收者 */
    public static final String KEY_AUD = "aud";

    /* 签发时间 */
    public static final String KEY_ISSUED_AT = "iat";
    /* 过期时间 */
    public static final String KEY_EXPIRES_AT = "exp";
    /* 生效时间 */
    public static final String KEY_NOT_BEFORE = "nbf";

    @Setter
    @Getter
    private Map<String, Object> claims = new HashMap<>();

    public Payload setId(String jti) {
        _data.put(KEY_ID, jti);
        return this;
    }

    public String getId() {
        return (String) _data.get(KEY_ID);
    }

    public Payload setSubject(String sub) {
        _data.put(KEY_SUB, sub);
        return this;
    }

    public String getSubject() {
        return (String) _data.get(KEY_SUB);
    }

    public Payload setIssuer(String iss) {
        _data.put(KEY_ISS, iss);
        return this;
    }

    public String getIssuer() {
        return (String) _data.get(KEY_ISS);
    }

    public Payload setIssuedAt(Date iat) {
        _data.put(KEY_ISSUED_AT, iat);
        return this;
    }

    public Date getIssuedAt() {
        return (Date) _data.get(KEY_ISSUED_AT);
    }

    public Payload setExpiresAt(Date exp) {
        _data.put(KEY_EXPIRES_AT, exp);
        return this;
    }

    public Date getExpiresAt() {
        return (Date) _data.get(KEY_EXPIRES_AT);
    }

    public Payload setNotBefore(Date nbf) {
        _data.put(KEY_NOT_BEFORE, nbf);
        return this;
    }

    public Date getNotBefore() {
        return (Date) _data.get(KEY_NOT_BEFORE);
    }
}
